home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac Magazin/MacEasy 19
/
Mac Magazin and MacEasy Magazine CD - Issue 19.iso
/
Musik & Kunst
/
Ear Workout 2.1
/
source code
/
complete_window.cp
next >
Wrap
Text File
|
1995-12-03
|
10KB
|
342 lines
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <OSUtils.h>
#include <QuickDraw.h>
#include <Sound.h>
#define NEED_MAC_STUFF 1
#include "Ninkasi:C++ util:generic.h"
#include "Ninkasi:C++ util:complete_window.h"
void copy_pascal_string(Str255,Str255);
class complete_window;
void dummy_den_mother(complete_window *x);
complete_window::complete_window(
int dlog_resource_id,
Str255 the_param_text0,
Str255 the_param_text1,
Str255 the_param_text2,
Str255 the_param_text3,
DEN_MOTHER_T *the_den_mother)
{
copy_pascal_string(param_text0,the_param_text0);
copy_pascal_string(param_text1,the_param_text1);
copy_pascal_string(param_text2,the_param_text2);
copy_pascal_string(param_text3,the_param_text3);
is_valid = 0;
resource_error = 0;
is_drawn = 0;
has_den_mother = VALID_POINTER(the_den_mother);
if (!has_den_mother)
den_mother = &dummy_den_mother; //--make extra sure we don't mess up
else
den_mother = the_den_mother;
ParamText(param_text0,param_text1,param_text2,param_text3);
the_window = GetNewDialog(
(short) dlog_resource_id,
(Ptr) 0, // allocate non-relocatable one for me
(WindowPtr) -1 // in front
);
if (!VALID_POINTER(the_window)) {
resource_error = ResError();
}
else {
is_valid = 1;
is_drawn = 1; // GetNewDialog draws it for me
whassup = complete_window_created;
if (VALID_POINTER(den_mother))
(*den_mother)(this);
}
}
complete_window::~complete_window()
{
whassup = complete_window_erase;
if (is_valid && VALID_POINTER(den_mother))
(*den_mother)(this);
if (VALID_POINTER(the_window))
DisposDialog(the_window);
}
void
dummy_den_mother(complete_window *x)
{
}
// This is a routine to help with keeping your part numbers straight in
// DITLs. The string "decoder" looks like, e.g.:
// check_boxes/0:5,ok_button,radio_buttons/8
// which defines part numbers 1-6 as check_boxes 0 through 5,
// part number 7 as ok_button, etc.
// "Decoder" is a Pascal string, just as read in from an 'STR ' resource using
// GetString.
// If 255 characters isn't enough for you, then instead you can use a string
// consisting of a zero length byte, then the string, then a C-style null
// terminator.
// "Nifty_name" is a C string.
// Returns -999 in *nifty_index if there's an error.
void
part_number_to_nifty_label(
char *nifty_name,
int *nifty_index,
char *decoder,
int part_num)
{
int current_part_num,decoder_len,current_place,slash,comma,colon,
have_slash,have_colon,first_nifty_index,last_nifty_index,
n_nifty_indices;
current_part_num = 0;
decoder_len = * (unsigned char *) decoder;
if (decoder_len==0) decoder_len=strlen(decoder+1);
current_place = 1; // index into decoder
while (current_place<=decoder_len) {
comma = find_char_in_string(decoder,current_place,decoder_len,',',1);
slash = find_char_in_string(decoder,current_place,comma-1,'/',1);
have_slash = (slash<=comma-1);
if (!have_slash) {
first_nifty_index = 1;
last_nifty_index = 1;
}
else {
colon = find_char_in_string(decoder,slash+1,comma-1,':',1);
have_colon = (colon<=comma-1);
if (!have_colon) {
first_nifty_index = 1;
last_nifty_index = substring_to_integer(decoder,slash+1,comma-1);
}
else {
first_nifty_index = substring_to_integer(decoder,slash+1,colon-1);
last_nifty_index = substring_to_integer(decoder,colon+1,comma-1);
}
}
n_nifty_indices = last_nifty_index - first_nifty_index + 1;
if (current_part_num + n_nifty_indices - 1 >= part_num) {
strncpy(nifty_name,decoder+current_place,slash-current_place);
nifty_name[slash-current_place] = '\0';
*nifty_index = first_nifty_index+part_num-current_part_num;
return;
}
current_place = comma+1;
current_part_num += n_nifty_indices;
}
*nifty_index = -999;
}
int
nifty_label_to_part_number(char *nifty_name,int nifty_index,char *decoder)
{
int current_part_num,decoder_len,current_place,slash,comma,colon,
have_slash,have_colon,first_nifty_index,last_nifty_index,
n_nifty_indices;
current_part_num = 1;
decoder_len = * (unsigned char *) decoder;
if (decoder_len==0) decoder_len=strlen(decoder+1);
current_place = 1; // index into decoder
while (current_place<=decoder_len) {
comma = find_char_in_string(decoder,current_place,decoder_len,',',1);
slash = find_char_in_string(decoder,current_place,comma-1,'/',1);
have_slash = (slash<=comma-1);
if (!have_slash) {
first_nifty_index = 1;
last_nifty_index = 1;
}
else {
colon = find_char_in_string(decoder,slash+1,comma-1,':',1);
have_colon = (colon<=comma-1);
if (!have_colon) {
first_nifty_index = 1;
last_nifty_index = substring_to_integer(decoder,slash+1,comma-1);
}
else {
first_nifty_index = substring_to_integer(decoder,slash+1,colon-1);
last_nifty_index = substring_to_integer(decoder,colon+1,comma-1);
}
}
n_nifty_indices = last_nifty_index - first_nifty_index + 1;
if (slash-current_place==strlen(nifty_name)
&& strncmp(nifty_name,decoder+current_place,slash-current_place)==0) {
return current_part_num + nifty_index - first_nifty_index;
}
current_place = comma+1;
current_part_num += n_nifty_indices;
}
return -999;
}
int
substring_to_integer(char *string,int first,int last)
{
int i;
char s[30];
if (last-first>29 || last-first<0) return 0;
for (i=first; i<=last; i++) {
s[i-first] = string[i];
}
s[last-first+1] = '\0';
return atoi(s);
}
// if_not_found = 1 says return last+1 if not found
// if_not_found = 2 says return -1 if not found
int
find_char_in_string(char *the_string,int first,int last,char the_char,int if_not_found)
{
int i;
for (i=first; i<=last; i++) {
if (the_string[i]==the_char) return i;
}
switch(if_not_found) {
case 1: return last+1;
case 2: return -1;
default: return -999;
}
}
void
set_ctl_by_nifty_label(WindowPtr w,char *nifty_label,int nifty_index,
char *decoder_string,int value)
{
int p;
short tt;
Handle hh;
Rect rr;
p = nifty_label_to_part_number(
nifty_label,nifty_index,decoder_string);
if (p != -999) {
GetDItem(w,(short) p,&tt,&hh,&rr);
if (VALID_HANDLE(hh))
SetCtlValue((ControlRecord **) hh,value);
}
}
void
get_rect_by_nifty_label(WindowPtr w,char *nifty_label,int nifty_index,
char *decoder_string,Rect *rr)
{
int p;
short tt;
Handle hh;
p = nifty_label_to_part_number(
nifty_label,nifty_index,decoder_string);
if (p != -999)
GetDItem(w,(short) p,&tt,&hh,rr);
else
SetRect(rr,(short) 9000,(short) 9000,(short) 9100,(short) 9100);
}
void
get_item_by_nifty_label(WindowPtr w,char *nifty_label,int nifty_index,
char *decoder_string,Handle *h)
{
int p;
short tt;
Rect rr;
p = nifty_label_to_part_number(
nifty_label,nifty_index,decoder_string);
if (p != -999)
GetDItem(w,(short) p,&tt,h,&rr);
else
*h = (Handle) 0;
}
void
show_ctl_by_nifty_label(WindowPtr w,char *nifty_label,int nifty_index,
char *decoder_string)
{
int p;
short tt;
Handle hh;
Rect rr;
p = nifty_label_to_part_number(
nifty_label,nifty_index,decoder_string);
if (p != -999) {
ShowDItem(w,(short) p);
}
}
void
hide_ctl_by_nifty_label(WindowPtr w,char *nifty_label,int nifty_index,
char *decoder_string)
{
int p;
short tt;
Handle hh;
Rect rr;
p = nifty_label_to_part_number(
nifty_label,nifty_index,decoder_string);
if (p != -999) {
HideDItem(w,(short) p);
}
}
void
activate_ctl_by_nifty_label(WindowPtr w,char *nifty_label,int nifty_index,
char *decoder_string)
{
int p;
short tt;
Handle hh;
Rect rr;
get_item_by_nifty_label(w,nifty_label,nifty_index,
decoder_string,&hh);
if (VALID_HANDLE(hh)) {
HiliteControl((ControlHandle) hh,(short) 0);
}
}
void
deactivate_ctl_by_nifty_label(WindowPtr w,char *nifty_label,int nifty_index,
char *decoder_string)
{
int p;
short tt;
Handle hh;
Rect rr;
get_item_by_nifty_label(w,nifty_label,nifty_index,
decoder_string,&hh);
if (VALID_HANDLE(hh)) {
HiliteControl((ControlHandle) hh,(short) 255);
}
}
void
refresh_text(complete_window *my_complete_window,
char *nifty_label,int nifty_index,char *decoder_string)
{
Handle h;
Str255 s;
get_item_by_nifty_label(my_complete_window->the_window,
nifty_label,nifty_index,decoder_string,&h);
if (VALID_HANDLE(h)) {
GetIText(h,s);
SetIText(h,s);
}
}
void
message_to_den_mother(complete_window *the_complete_window,void **data_h,
void *data_p,int data_i,char *data_v)
{
if (!VALID_POINTER(the_complete_window)
|| !the_complete_window->is_valid
|| !VALID_POINTER(the_complete_window->den_mother))
return;
the_complete_window->whassup = complete_window_action;
the_complete_window->the_event = (EventRecord *) 0;
the_complete_window->part_code = -1;
the_complete_window->part_number = -1;
the_complete_window->data_handle = data_h;
the_complete_window->data_pointer = data_p;
the_complete_window->data_int = data_i;
the_complete_window->data_verb = data_v;
(*(the_complete_window->den_mother))(the_complete_window);
}